﻿using System;
using System.Collections.Generic;
using Csla;
using System.Data;
using ENT.GP.Datos.MysqlHelper;
using MySql.Data.MySqlClient;

namespace ENT.GP.Core.Listas
{
    [Serializable]
    public class CantonLista :
      ReadOnlyListBase<CantonLista, CantonInfo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(CantonLista), "Role");
        }

        #endregion

        #region Factory Methods

        public static CantonLista GetCantonLista()
        {
            return DataPortal.Fetch<CantonLista>();
        }

        private CantonLista()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch()
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySqlParameter[] parametros = new MySqlParameter[2];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO", "CANTONR");
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO2", "PROVINCIAP");
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "Listar_Catalogo_Codigos").Tables[0].Rows)
            {
                this.Add(CantonInfo.GetCantonInfo(items));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class CantonInfo : ReadOnlyBase<CantonInfo>
    {
        #region Business Methods

        public int Id { get; set; }
        public int IdCatalogoPadre { get; set; }
        public string Codigo { get; set; }
        public string Nombre { get; set; }
        public string Descripcion { get; set; }

        #endregion

        #region Factory Methods

        internal static CantonInfo GetCantonInfo(DataRow childData)
        {
            return DataPortal.FetchChild<CantonInfo>(childData);
        }

        private CantonInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            int id, idp;
            int.TryParse(childData[0].ToString(), out id);
            Id = id;
            Codigo = childData[1].ToString();
            Nombre = childData[2].ToString();
            Descripcion = childData[3].ToString();
            int.TryParse(childData[4].ToString(), out idp);
            IdCatalogoPadre = idp;
        }

        #endregion
    }
}
